/* 基本爬虫函数，用于房间初期维护 */

import { getDistance } from "@/utils"

/* 目前仅用于 建筑工、采集工、升级工 */
/* 负责采集、填充 */
export function harvest_(creep:Creep):void{
    if (!creep.memory) return
    var thisRoom = Game.rooms[creep.memory.belong]
    if (!thisRoom) return
    if (!thisRoom.memory.StructureIdData.source1ID) return
    if (!creep.memory.standed) creep.memory.standed = false
    /* 区分阶段 */
    creep.workstate('energy')
    if (creep.memory.working)
    {
        creep.memory.standed = false
        if (thisRoom.memory.StructureIdData.source_link)
        {
            var source_link = Game.getObjectById(thisRoom.memory.StructureIdData.source_link) as StructureLink
            if (!source_link)
            {
                delete thisRoom.memory.StructureIdData.source_link
                return
            }
            if (creep.transfer(source_link,'energy') == ERR_NOT_IN_RANGE)
                creep.goTo(source_link.pos,1)      
        }
        else
        {
            if (thisRoom.memory.StructureIdData.storageID)
            {
                var storage = Game.getObjectById(thisRoom.memory.StructureIdData.storageID) as StructureStorage
                if (!storage)
                {
                    delete thisRoom.memory.StructureIdData.storageID
                    return
                }
                if (creep.transfer(storage,'energy') == ERR_NOT_IN_RANGE)
                    creep.goTo(storage.pos,1)  
            }
            else
            {
                /* 都没有就直接寻找最近的extension 进行填充 */
                var extensions = creep.pos.findClosestByPath(FIND_MY_STRUCTURES,{filter:(structure)=>{
                    return (structure.structureType == 'extension' || structure.structureType == 'spawn' )&& structure.store.getFreeCapacity('energy') > 0
                }})
                if (extensions)
                {
                    if (creep.transfer(extensions,'energy') == ERR_NOT_IN_RANGE)
                    {
                        creep.goTo(extensions.pos,1)
                    }
                }
                else
                {
                    var construction = creep.pos.findClosestByPath(FIND_MY_CONSTRUCTION_SITES)
                    if (construction)
                    {
                        if (creep.build(construction) == ERR_NOT_IN_RANGE)
                        {
                            creep.goTo(construction.pos,1)
                            creep.memory.standed = false
                        }
                        if (getDistance(creep.pos,construction.pos) <= 3)
                        creep.memory.standed = true
                        return
                    }
                    /* 如果有防御塔就填防御塔 */
                    if (thisRoom.memory.StructureIdData.NtowerID)
                    {
                        var tower = Game.getObjectById(thisRoom.memory.StructureIdData.NtowerID) as StructureTower
                        if (!tower) {delete thisRoom.memory.StructureIdData.NtowerID;return}
                        if (tower.store.getFreeCapacity('energy') > 0)
                        {
                            if (creep.transfer(tower,'energy') == ERR_NOT_IN_RANGE)
                            {
                                creep.goTo(tower.pos,1)
                            }
                        }
                    }
                }
            }
        }
    }
    else
    {
        var source = Game.getObjectById(thisRoom.memory.StructureIdData.source1ID) as Source
        if (!source) return
        if (!creep.pos.isNearTo(source))
        {
            var voidpoint = source.pos.getSourceVoid()
            let boo = false
            for (var v of voidpoint)
            {
                if (!v.lookFor(LOOK_CREEPS)[0]) boo = true
            }
            if (boo)
            {
                creep.goTo(source.pos,1)
                creep.memory.standed = false
            }
        }
        else
        {
            var i = creep.pos.findInRange(FIND_DROPPED_RESOURCES,1)
            if (i.length > 0 && i[0].amount > 20 && i[0].resourceType == 'energy')
            {creep.pickup(i[0]);return}
            creep.harvest(source)
            creep.memory.standed = true
        }
    }
}
/* 负责建筑、维护 */
export function build_(creep:Creep):void{
    if (!creep.memory) return
    var thisRoom = Game.rooms[creep.memory.belong]
    if (!thisRoom) return
    if (!thisRoom.memory.StructureIdData.source1ID) return
    if (!creep.memory.standed) creep.memory.standed = false
    creep.workstate('energy')
    if (creep.memory.working)
    {
        var construction = creep.pos.findClosestByPath(FIND_MY_CONSTRUCTION_SITES)
        if (construction)
        {
            if (creep.build(construction) == ERR_NOT_IN_RANGE)
            {
                creep.goTo(construction.pos,1)
                creep.memory.standed = false
            }
            if (getDistance(creep.pos,construction.pos) <= 3)
            creep.memory.standed = true
        }
        else
        {
            /* 没有建筑物则考虑道路维护 */
            var roads = creep.pos.findClosestByPath(FIND_STRUCTURES,{filter:(structure)=>{
                return structure.structureType == 'road' && structure.hits < structure.hitsMax
            }})
            if (roads)
            {
                creep.say("🛠️",true)
                if (creep.repair(roads) == ERR_NOT_IN_RANGE)
                {
                    creep.goTo(roads.pos,1)
                }
                if (getDistance(creep.pos,roads.pos) <= 3)
                creep.memory.standed = false
            }
        }
    }
    else
    {
        creep.memory.standed = false
        /* 如果有storage就去storage里找，没有就自己采集 */
        if (thisRoom.memory.StructureIdData.storageID)
        {
            var storage = Game.getObjectById(thisRoom.memory.StructureIdData.storageID) as StructureStorage
            if (!storage)
            {
                delete thisRoom.memory.StructureIdData.storageID
                return
            }
            if (creep.withdraw(storage,'energy') == ERR_NOT_IN_RANGE)
                creep.goTo(storage.pos,1)  
        }
        else
        {
            var source = creep.pos.findClosestByPath(FIND_SOURCES)
            if (source)
            {
                if (!creep.pos.isNearTo(source))
                {
                    creep.goTo(source.pos,1)
                    creep.memory.standed = false
                }
                else
                {
                    creep.harvest(source)
                    creep.memory.standed = true
                }
                
            }
        }
    }

}
/* 负责升级 */
export function upgrade_(creep:Creep):void{
    if (!creep.memory) return
    var thisRoom = Game.rooms[creep.memory.belong]
    if (!thisRoom) return
    if (!thisRoom.memory.StructureIdData.source1ID) return
    if (!creep.memory.standed) creep.memory.standed = false
    creep.workstate('energy')
    if (creep.memory.working)
    {
        var thisRoom = Game.rooms[creep.memory.belong]
        if (thisRoom.MissonNum('Creep','控制冲级')>0 && thisRoom.memory.StructureIdData.source2_link)
        {
            var source2_link = Game.getObjectById(thisRoom.memory.StructureIdData.source2_link) as StructureLink
            if (!source2_link) {delete thisRoom.memory.StructureIdData.source2_link;return}
            if (!creep.pos.isNearTo(source2_link)) creep.goTo(source2_link.pos,1)
            else creep.transfer(source2_link,'energy')
            return
        }
        if(thisRoom.MissonNum('Creep','控制冲级')<=0 && thisRoom.controller.level >= 8)
        {
            if (creep.room.controller.ticksToDowngrade < 190000 || !creep.room.memory.StructureIdData.source2_link)
            {
                creep.upgrade__()
                return
            }   
            var source2_link = Game.getObjectById(thisRoom.memory.StructureIdData.source2_link) as StructureLink
            if (!source2_link) {delete thisRoom.memory.StructureIdData.source2_link;return}
            if (!creep.pos.isNearTo(source2_link)) creep.goTo(source2_link.pos,1)
            else creep.transfer(source2_link,'energy')
            return
        }
        if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE)
        {
            creep.goTo(creep.room.controller.pos,2)
            creep.memory.standed = false
        }
        if (creep.pos.inRangeTo(creep.room.controller.pos.x,creep.room.controller.pos.y,4))
        {
            creep.memory.standed = true
        }
    }
    else
    {
        var source = Game.getObjectById(thisRoom.memory.StructureIdData.source2ID) as Source
        if (!source) return
        if (!creep.pos.isNearTo(source))
        {
            var voidpoint = source.pos.getSourceVoid()
            let boo = false
            for (var v of voidpoint)
            {
                if (!v.lookFor(LOOK_CREEPS)[0]) boo = true
            }
            if (boo)
            {
                creep.goTo(source.pos,1)
                creep.memory.standed = false
            }
        }
        else
        {
            var i = creep.pos.findInRange(FIND_DROPPED_RESOURCES,1)
            if (i.length > 0 && i[0].amount > 20 && i[0].resourceType == 'energy')
            {creep.pickup(i[0]);return}
            creep.harvest(source)
            creep.memory.standed = true
        } 
    }
}